package com.hongyuan.sellfitness.ui.main.activity;

import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hongyuan.mvvmhabitx.bus.RxBus;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.databinding.ActivityScanBinding;
import com.hongyuan.sellfitness.ui.main.beans.ScanBeans;
import com.hongyuan.sellfitness.utils.CustomDialog;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import me.tatarka.bindingcollectionadapter2.BR;

public class ScanActivity extends CustomActivity<ActivityScanBinding, CustomViewModel> implements QRCodeView.Delegate{

    //灯光是否打开
    private boolean isOpen;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_scan;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initView() {
        super.initView();

        binding.mQRCodeView.setDelegate(this);
        binding.openLight.setOnClickListener(v -> {
            if(!isOpen){
                binding.mQRCodeView.openFlashlight();//开灯
                isOpen = true;
            }else{
                binding.mQRCodeView.closeFlashlight();//关灯
                isOpen = false;
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        binding.mQRCodeView.startCamera();//打开相机
        binding.mQRCodeView.startSpotAndShowRect();//显示扫描框并开始识别
    }

    @Override
    protected void onStop() {
        binding.mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        binding.mQRCodeView.onDestroy();
        super.onDestroy();
    }


    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
        try {
            ScanBeans beans = new Gson().fromJson(result, new TypeToken<ScanBeans>(){}.getType());
            if("cs_qd".equals(beans.getHref_code())){
                //团课签到
                RxBus.getDefault().post(beans);

                finish();
            }else{
                showPromptDialog();
            }
        }catch (Exception e){
            e.printStackTrace();
            showPromptDialog();
        }
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {
//        这里是通过修改提示文案来展示环境是否过暗的状态，接入方也可以根据 isDark 的值来实现其他交互效果
//        String tipText = binding.mQRCodeView.getScanBoxView().getTipText();
//        String ambientBrightnessTip = "\n环境过暗，请打开闪光灯";
//        if (isDark) {
//            if (!tipText.contains(ambientBrightnessTip)) {
//                binding.mQRCodeView.getScanBoxView().setTipText(tipText + ambientBrightnessTip);
//            }
//        } else {
//            if (tipText.contains(ambientBrightnessTip)) {
//                tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
//                binding.mQRCodeView.getScanBoxView().setTipText(tipText);
//            }
//        }
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        //扫码失败
    }

    /*
     * 抖动效果
     * */
    private void vibrate() {
        Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    /*
     * 弹框提示处理
     * */
    private void showPromptDialog(){
        CustomDialog.promptDialog(this,
                "抱歉，二维码有误，是否在尝试一次？",
                "确定",
                "取消", true,
                v -> {
                    if(v.getId() == R.id.isOk){
                        binding.mQRCodeView.startSpot();
                    }else{
                        finish();
                    }
                });
    }
}
